package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.ConsultNotice;
import cn.xgj.sfzy.service.ConsultNoticeService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName consultNoticeController
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月22日 下午7:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/consultNotice")
public class ConsultNoticeController {
    @Autowired
    private ConsultNoticeService consultNoticeService;

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(ConsultNotice consultNotice) {
        List<ConsultNotice> list = consultNoticeService.findAll(consultNotice);
        return Result.success(list);
    }

    //    分页
    @RequestMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "2") Integer pageSize,
            ConsultNotice consultNotice) {
        PageInfo<ConsultNotice> byPage = consultNoticeService.findByPage(pageNum, pageSize, consultNotice);
        return Result.success(byPage);
    }

    //添加
    @PostMapping
    public Result add(@RequestBody ConsultNotice consultNotice) {
        int num = consultNoticeService.add(consultNotice);
        if (num != 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    //修改
    @PutMapping
    public Result update(@RequestBody ConsultNotice consultNotice) {
        int num = consultNoticeService.edit(consultNotice);
        if (num != 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    //删除
    @DeleteMapping("/{noticeId}")
    public Result delete(@PathVariable(value = "noticeId") Integer noticeId) {
        int num = consultNoticeService.remove(noticeId);
        if (num != 0) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
}
